﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TelephoneYiShou.XQL
{
    public partial class FrmGoodsType : Skin_Mac
    {
        public FrmGoodsType()
        {
            InitializeComponent();
        }
       
        private void FrmGoodsType_Load(object sender, EventArgs e)
        {
            string sql = "select * from CommodityType";
            DataTable dt = DBHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
        //添加
        private void buttadd_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string ssql = string.Format("insert into commoditytype values('{0}')",name);
            if (string.IsNullOrEmpty(name))
            {
                MessageBoxEx.Show("请填写名称");
            }
            else
            {
                DialogResult o = MessageBoxEx.Show("确定添加" + name + "吗？", "提示", MessageBoxButtons.YesNo);
                if (o==DialogResult.Yes)
                {
                    if (DBHelper.ExecuteNonQuery(ssql))
                    {
                        MessageBoxEx.Show("添加成功");
                        string sql = "select * from CommodityType";
                        DataTable dt = DBHelper.GetDataTable(sql);
                        dataGridView1.DataSource = dt;
                    }
                    else
                    {
                        MessageBoxEx.Show("添加失败");
                    }
                }
            }
        }
        //点击名称显示到txt上
        private void dataGridView1_Click(object sender, EventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            labid.Text = "您当前选择的类型编号为"+ dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        }
        //修改
        private void buttupdate_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string typeids = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string ssql = string.Format("update commoditytype set typename = '{0}'where typeid = '{1}'",name,typeids);
            if (string.IsNullOrEmpty(name))
            {
                MessageBoxEx.Show("请填写名称");
            }
            else
            {
                DialogResult o = MessageBoxEx.Show("确定修改为" + name + "吗？", "提示", MessageBoxButtons.YesNo);
                if (o == DialogResult.Yes)
                {
                    if (DBHelper.ExecuteNonQuery(ssql))
                    {
                        MessageBoxEx.Show("修改成功");
                        string sql = "select * from CommodityType";
                        DataTable dt = DBHelper.GetDataTable(sql);
                        dataGridView1.DataSource = dt;
                    }
                    else
                    {
                        MessageBoxEx.Show("修改失败");
                    }
                }
            }
        }
        //删除
        private void buttdetele_Click(object sender, EventArgs e)
        {
            string typeids = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string ssql = string.Format("delete commoditytype where typeid = '{0}'",  typeids);
            string name = textBox1.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBoxEx.Show("请填写名称");
            }
            else
            {
                DialogResult o = MessageBoxEx.Show("确定删除" + name + "吗？", "提示", MessageBoxButtons.YesNo);
                if (o == DialogResult.Yes)
                {
                    try
                    {
                        if (DBHelper.ExecuteNonQuery(ssql))
                        {
                            MessageBoxEx.Show("删除成功");
                            string sql = "select * from CommodityType";
                            DataTable dt = DBHelper.GetDataTable(sql);
                            dataGridView1.DataSource = dt;
                        }
                        else
                        {
                            MessageBoxEx.Show("删除失败");
                        }
                    }
                    catch (Exception)
                    {

                        MessageBoxEx.Show("请先删除该品牌的商品");
                    }
                    
                }
            }
        }
        //退出系统
        private void button_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
